﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AUV5.Web.Manage
{
    public partial class ServiceManage : PageBaseBackend
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["username"] != null)
                {
                    txtUserName.Text = Request["username"].ToString();
                }
                if (Request["isdel"] != null)
                {
                    cbxIsDel.Checked = bool.Parse(Request["isdel"]);
                }
                this.BindList();
            }
        }

        private void BindList()
        {
            string where="IsDel="+(cbxIsDel.Checked?((int)Common.Enum.auvDel.Del).ToString():((int)Common.Enum.auvDel.NotDel).ToString());
            if (txtUserName.Text.Trim() != "")
            {
                where += " and UserID='" + BLL.UserInfoBLL.Instance.GetUserIdByUserName(txtUserName.Text.Trim())+"'"; 
            }
            Model.NetPager pager = new Model.NetPager();
            pager.PageIndex = AspNetPager1.CurrentPageIndex;
            pager.TableName = "ServiceInfo";
            pager.PrimaryKey = "ServiceID";
            pager.Where = where;
            pager.Order = "UpdateDate desc";
            pager.SortType = (int)Model.NetPager.SqlSortType.Desc;
            pager.PageSize = AspNetPager1.PageSize;
            IList<AUV5.Model.ServiceInfo> list = AUV5.BLL.ServiceInfoBLL.Instance.GetServiceInfoListPage(pager);
            repService.DataSource = list;
            repService.DataBind();
            AspNetPager1.RecordCount = pager.TotalCount;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            this.BindList();
        }

        protected void repService_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //推荐
            LinkButton lbtnFeature = (LinkButton)e.Item.FindControl("lbtnFeature");
            //启用
            LinkButton lbtnEnable = (LinkButton)e.Item.FindControl("lbtnEnable");
            //禁用
            LinkButton lbtnDisable = (LinkButton)e.Item.FindControl("lbtnDisable");
            //删除
            LinkButton lbtnDelete = (LinkButton)e.Item.FindControl("lbtnDelete");
            //撤消删除
            LinkButton lbtnRecover = (LinkButton)e.Item.FindControl("lbtnRecover");
            string serviceid = ((HiddenField)e.Item.FindControl("hfServiceID")).Value ;

            Model.ServiceInfo service = BLL.ServiceInfoBLL.Instance.GetDetail(serviceid);
            if (service != null)
            {
                if (service.IsDel == (int)Common.Enum.auvDel.NotDel)
                {
                    if (service.StatusID != (int)Common.Enum.ServiceStatus.Featured)
                    {
                        lbtnFeature.Visible = true;
                    }
                    if (service.StatusID == (int)Common.Enum.ServiceStatus.Denied)
                    {
                        lbtnEnable.Visible = true;
                    }
                    if (service.StatusID != (int)Common.Enum.ServiceStatus.Denied)
                    {
                        lbtnDisable.Visible = true;
                    }
                    lbtnDelete.Visible = true;
                }
                else
                {
                    lbtnRecover.Visible = true;
                }
            }
        }

        /// <summary>
        /// 更改状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;

            BLL.ServiceInfoBLL.Instance.ChangeStatus(ddl.ToolTip, (int)Enum.Parse(typeof(Common.Enum.ServiceStatus), ddl.SelectedValue));

            BindList();
        }

        //查找
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindList();
        }

        //执行对服务的管理操作
        protected void repService_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string serviceid = e.CommandArgument.ToString();
            Model.ServiceInfo service = BLL.ServiceInfoBLL.Instance.GetDetail(serviceid);
            switch (e.CommandName)
            {
                case "Feature":
                    service.StatusID = (int)Common.Enum.ServiceStatus.Featured;
                    break;
                case "Enable":
                    service.StatusID = (int)Common.Enum.ServiceStatus.Active;
                    break;
                case "Disable":
                    service.StatusID = (int)Common.Enum.ServiceStatus.Denied;
                    break;
                case "Delete":
                    service.IsDel = (int)Common.Enum.auvDel.Del;
                    break;
                case "Recover":
                    service.IsDel = (int)Common.Enum.auvDel.NotDel;
                    break;
                default:
                    break;
            }
            BLL.ServiceInfoBLL.Instance.Update(service);

            BindList();
        }
    }
}